home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-06-22 | 3.4 KB | 113 lines | [TEXT/PJMM] |
- {Written in Lightspeed Pascal}
-
- {This unit was written by Andrew Welch (A.Welch1 on GEnie)}
- {It is completely free for all to use, but the author assumes}
- {no responsibility for any bugs in this code. It has been tested}
- {and used quite extensively, but you never know!}
-
- {This unit consist of one procedure that your program 'sees'}
- {called CenterDLOG. You pass CenterDLOG a DialogPtr or a}
- {WindowPtr and CenterDLOG centers the dialog or window over}
- {the frontmost window, or if there are no windows, it is just}
- {centered on the screen.}
-
- {The purpose of the unit is to provide some standard way that Mac}
- {programmers out there can use to implement this convenient feature}
- {in all of their programs. This routine is great for users who have big}
- {screens and hate to have to move their mouse all over the place to}
- {answer a dialog, because the dialogs are centered over the current}
- {(frontmost) window, where they are most likely to be working.}
- {It is also great for those people who have more than one monitor,}
- {as the dialogs are centered on the screen they are working on.}
-
- {This routine is also great for you programmer types, because you}
- {don't have to worry about where the dialogs appear ("Will this fit on}
- {a Mac SE's screen?!?!?"), just call this routine and they will come}
- {up looking pretty no matter what computer the user is running.}
- {CenterDLOG is smart enough to make sure that the dialog fits on the}
- {screen after it is centered.}
-
- {Here's an example of how you could use it: (note that although I}
- {refer to Dialogs here, the same can be done with Windows.}
-
- { MyDialog:=GetNewDialog(…); The dialog should NOT be marked as visible! }
- { CenterDLOG(MyDialog); …this prevents the user from seeing the dialog}
- { ShowWindow(MyDialog); jumping all over the screen}
-
- {Quick, easy, and worth it. Enjoy!}
-
- unit Centerer;
-
- interface
-
- procedure CenterDLOG (adialog: dialogptr);
-
- implementation
-
- function GetGreyRgn: rgnhandle;
- var
- ourrgn: rgnhandle;
- thePtr: ^rgnhandle;
-
- begin
- thePtr := pointer($9EE);
- ourrgn := thePtr^;
- getgreyrgn := ourRgn;
- end;
-
- procedure CenterDlog;
-
- var
- wmport, saveport: grafptr;
- dwidth, dheight, wheight, wwidth, xplus, yplus, x, y: integer;
- arect: rect;
- thergn: rgnhandle;
- thewindow: windowptr;
-
- begin
- thewindow := frontwindow;
- getwmgrport(wmport);
- if thewindow <> nil then
- begin
- arect := thewindow^.portrect;
- getport(saveport);
- setport(thewindow);
- localtoglobal(arect.topleft);
- xplus := arect.left;
- yplus := arect.top;
- setport(saveport);
- wwidth := thewindow^.portrect.right - thewindow^.portrect.left;
- wheight := thewindow^.portrect.bottom - thewindow^.portrect.top;
- end
- else
- begin
- xplus := 0;
- yplus := 0;
- wwidth := wmport^.portrect.right - wmport^.portrect.left;
- wheight := wmport^.portrect.bottom - wmport^.portrect.top;
- end;
- arect := adialog^.portrect;
- insetrect(arect, -5, -5);
- dwidth := arect.right - arect.left;
- dheight := arect.bottom - arect.top;
- x := (wwidth - dwidth) div 2;
- y := (wheight - dheight) div 2;
- x := x + xplus + 5;
- y := y + yplus + 5;
- thergn := getgreyrgn;
- xplus := (x + dwidth) - thergn^^.rgnbbox.right;
- if xplus > 0 then
- x := x - xplus;
- xplus := x;
- if xplus < 10 then
- x := 10;
- yplus := (y + dheight) - thergn^^.rgnbbox.bottom;
- if yplus > 0 then
- y := y - yplus;
- yplus := y;
- if yplus < 30 then
- y := 30;
- movewindow(adialog, x, y, false);
- end;
-
- end.